﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Web.Configuration;
using HospitalSystem.Core;
namespace HospitalSystem
{
    public partial class showcidinfo3 : System.Web.UI.Page
    {
        public static List<string> y = new List<string>();
        string caseNumber = "";
        private show_Image _showimage;
        protected void Page_Load(object sender, EventArgs e)
        {
            showsession();
            _showimage = new show_Image();
            _showimage.show_Images(cid_label, Image1);
        }
        protected void showsession()
        {
            String ShowValue = (string)Session["CID"];
            cid_label.Text = ShowValue;
            String ShowValue2 = (string)Session["LastName"];
            lname.Text = ShowValue2;

            String ShowValue3 = (string)Session["FirstName"];
            fname.Text = ShowValue3;


            //String ShowValue4 = (string)Session["CID"];
            //SelectStat.Text = ShowValue4;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            checkcaseno();
            
            //bindcasebo();
        }

        protected void checkcaseno()
        {
    
          
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);
            //ต้องการแก้เคสที่มีอยู่แล้ว
            scon.Open();
            SqlCommand cmd = new SqlCommand("SELECT CID from Evaluation where CID = @sj", scon);
            cmd.Parameters.AddWithValue("@sj", cid_label.Text);
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.Read() != false)
            {
                bindcasebo();
            }

            else
            {
                
                MessageBox("เด็กรายได้ยังไม่ได้ทำการคัดกรองเราจะพาคุณไปหน้าทำการคัดกรอง", "cal.aspx");
            
            }
            reader.Close();
           
        }



        private void MessageBox(string msg, string urld)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "');" + "window.location.href='" + urld + "';" + "</script>";
            Page.Controls.Add(lbl);
        }


        protected void bindcasebo()
        {
            y.Clear();
            string CID = (String)Session["CID"];
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);
            //ต้องการแก้เคสที่มีอยู่แล้ว
            scon.Open();
            SqlCommand cmd = new SqlCommand("SELECT Person.CID, Evaluation.CaseNo, Person.FirstName, Person.LastName FROM Person INNER JOIN Evaluation ON Person.CID = Evaluation.CID where Person.CID = @sj ORDER BY Evaluation.CaseNo", scon);
            cmd.Parameters.AddWithValue("@sj", CID);
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read() != false)
            {
                y.Add(reader["CaseNo"].ToString());
            }
            reader.Close();
            for (int i = 0; i < y.Count(); i++)
            {
                caseNumber = y[i].ToString();
            }
            scon.Close();
            Session["CaseNo"] = caseNumber;

            Response.Redirect("evaluationscreening.aspx");
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/plan.aspx");
        }
    }
}